package com.monlyu.tag.extend;

import com.monlyu.tag.BaseTag;
import com.monlyu.util.ActionUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.util.WebOptUtil;

/********************************************************************************
 * 
 * <b>功能描述☞</b>这个是用于网页上图形链接的标签,比如 :<br>
 * <code><a><img src="http://monlyu.javaeye.com/upload/picture/pic/52702/7f221a45-f376-3d41-bbc4-b8d0aa583f66.png" /></a></code>
 * <br>
 * 
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>monlyu<br>
 * <b>文件路径☞</b>com.monlyu.tag.extend.ImgATag.java<br>
 * <b>开发日期☞</b>2010-1-14 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version : 1.0.5
 * 
 ********************************************************************************/
public class ImgATag extends BaseTag {

	@Override
	public int doStartTag() {
		write2Page(buildTag());
		return SKIP_BODY;
	}

	private String buildTag() {
		StringBuilder builder = new StringBuilder();
		builder.append("<a ");
		if (!StringUtil.isTrimEmpty(msg)) {
			builder.append(" title='" + msg + "'");
		}
		if (!StringUtil.isTrimEmpty(action)) {
			int inw = action.indexOf("?");
			if (inw < 0) {
				href = WebOptUtil.APP_ARGS.getWebRelUrl() + action + ActionUtil.ACTINON_END;
			} else {
				String getatin = action.substring(0, inw);
				href = WebOptUtil.APP_ARGS.getWebRelUrl() + getatin + ActionUtil.ACTINON_END + action.substring(inw);
			}
		}
		if (!StringUtil.isTrimEmpty(href)) {
			builder.append(" href='" + href + "'");
		}
		if (!StringUtil.isTrimEmpty(onclick)) {
			builder.append(" onclick='" + onclick + "'");
		}
		builder.append(">");
		builder.append(getImg(img));
		builder.append("</a>");
		return builder.toString();
	}

	/**
	 * 获取图片的
	 */
	private StringBuilder getImg(String image) {
		StringBuilder sb = new StringBuilder("<img class='nb ");
		if (!StringUtil.isTrimEmpty(cssClass)) {
			sb.append(cssClass);
		}
		sb.append("' ");
		if (!StringUtil.isTrimEmpty(id)) {
			sb.append(" id='" + id + "'");
		}
		sb.append(" src='");
		sb.append(WebOptUtil.APP_ARGS.getWebRelUrl() + "common/images/optImg/");
		sb.append(StringUtil.trim(image));
		if (!image.endsWith(".png")) {
			sb.append(".png");
		}
		sb.append("'>");
		return sb;
	}

	private String img;
	private String onclick;
	private String href;
	private String msg;
	private String id;
	private String cssClass;
	private String action;

	public void setAction(String action) {
		this.action = action;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public void setImg(String img) {
		this.img = img;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	/** 获取菜单的图片 */
	public static final String menuImg(String image) {
		return WebOptUtil.APP_ARGS.getWebRelUrl() + "/common/images/menu/" + image;
	}
}
